//
//  H_CoreDataDAL.m
//  Honey
//
//  Created by 贾亚涵 on 16/4/20.
//  Copyright © 2016年 贾亚涵. All rights reserved.
//

#import "H_CoreDataDAL.h"
#import "AppDelegate.h"
#import "H_UsersInfo.h"
#import "H_BingDInfo.h"
#import "H_userHeadPic.h"

@interface H_CoreDataDAL ()
@property(nonatomic,retain)AppDelegate * appDele;

@end
@implementation H_CoreDataDAL
//APPDelegate初始化
-(instancetype)init
{
    self = [super init];
    if (self)
    {
        _appDele = [[UIApplication sharedApplication] delegate];
    }
    return self;
}
//用户-手机号重复检查结果  YES :重复  NO：不重复
-(BOOL)checkRepeatedlyPhoneNumber:(NSString *)phoneN
{
    //创建查询请求(请求需要实体和条件)
    NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
    //创建实体
    NSEntityDescription * entity = [NSEntityDescription entityForName:@"H_UsersInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fetchRequest setEntity:entity];
    //创建查询条件
    NSString * str = [NSString stringWithFormat:@"phone LIKE '%@'",phoneN];
    //设置谓词条件
    fetchRequest.predicate = [NSPredicate predicateWithFormat:str];
    
    NSArray * arr = [_appDele.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    if(arr.count == 0)
    {//没查到
        return NO;
    }
    return YES;
}
//用户-昵称重复检查结果
-(BOOL)checkRepeatedlyNick:(NSString *)nick
{
    NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription * entity = [NSEntityDescription entityForName:@"H_UsersInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fetchRequest setEntity:entity];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"nick LIKE '%@'",nick]];
    NSArray * arr = [_appDele.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    if (arr.count == 0)
    {
        return NO;
    }
    return YES;
}
//用户-注册信息存入
-(void)saveRegisterInfo:(NSString *)phone :(NSString *)nick :(NSString *)password
{
    NSEntityDescription * entity = [NSEntityDescription entityForName:@"H_UsersInfo" inManagedObjectContext:_appDele.managedObjectContext];
    H_UsersInfo * usersInfo = [[H_UsersInfo alloc] initWithEntity:entity insertIntoManagedObjectContext:_appDele.managedObjectContext];
    
    usersInfo.phone = phone;
    usersInfo.nick = nick;
    usersInfo.password = password;
    
    
    [_appDele saveContext];
    
    NSEntityDescription * entity2= [NSEntityDescription entityForName:@"H_BingDInfo" inManagedObjectContext:_appDele.managedObjectContext];
    H_BingDInfo * usersInfo2 = [[H_BingDInfo alloc] initWithEntity:entity2 insertIntoManagedObjectContext:_appDele.managedObjectContext];
    
    usersInfo2.phone = phone;
    usersInfo2.bingDphone = nil;
    usersInfo2.isMsg = @"3";
    [_appDele saveContext];
    
    NSEntityDescription * entity3= [NSEntityDescription entityForName:@"H_userHeadPic" inManagedObjectContext:_appDele.managedObjectContext];
    H_userHeadPic * usersInfo3 = [[H_userHeadPic alloc] initWithEntity:entity3 insertIntoManagedObjectContext:_appDele.managedObjectContext];
    
    usersInfo3.phone = phone;
    //默认头像
    UIImage * ima = [UIImage imageNamed:@"MoreP1"];
    NSData * data = UIImageJPEGRepresentation(ima, 1.0);
    usersInfo3.headPic = data;
    
    [_appDele saveContext];
}

//用户-登录验证
-(BOOL)checkLoginInfoPhoneNum:(NSString *)Phone PassWord :(NSString *)password
{
    NSFetchRequest * fecthR = [NSFetchRequest new];
    //请求实体
    NSEntityDescription * entityD = [NSEntityDescription entityForName:@"H_UsersInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR setEntity:entityD];
    //创建查询条件
    NSString * str = [NSString stringWithFormat:@"phone LIKE '%@' && password LIKE '%@'",Phone,password];
    //设置谓词条件
    fecthR.predicate = [NSPredicate predicateWithFormat:str];
    NSArray * arr = [_appDele.managedObjectContext executeFetchRequest:fecthR error:nil];
    if(arr.count == 0)
    {//没查到
        return NO;
    }
    return YES;
}

//绑定用户验证
-(BOOL)checkBingDInfoPhoneNum:(NSString *)Phone
{
    NSFetchRequest * fecthR1 = [NSFetchRequest new];
    NSEntityDescription * entityD1 = [NSEntityDescription entityForName:@"H_BingDInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR1 setEntity:entityD1];
    NSString * str1 = [NSString stringWithFormat:@"phone LIKE '%@'",Phone];
    fecthR1.predicate = [NSPredicate predicateWithFormat:str1];
    NSArray * arr1 = [_appDele.managedObjectContext executeFetchRequest:fecthR1 error:nil];
    if (arr1.count == 0)
    {
        return NO;
    }
    else{
        H_BingDInfo *model = arr1[0];
        if (model.bingDphone == nil) {
            return YES;
        }
        return NO;
    }
}

//用户-查询是否绑定手机号,0：成功绑定 1：发送申请 2：接受申请 3：未绑定
-(int)checkIsBingDPhoneNum:(NSString *)Phone
{
    NSFetchRequest * fecthR = [NSFetchRequest new];
    NSEntityDescription * entityD = [NSEntityDescription entityForName:@"H_BingDInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR setEntity:entityD];
    NSString * str1 = [NSString stringWithFormat:@"phone LIKE '%@'",Phone];
    fecthR.predicate = [NSPredicate predicateWithFormat:str1];
    NSArray * arr1 = [_appDele.managedObjectContext executeFetchRequest:fecthR error:nil];
    H_BingDInfo *model = arr1[0];
    if ([model.isMsg isEqualToString:@"3"])
    {
        return 3;
    }else if ([model.isMsg isEqualToString:@"2"])
    {
        return 2;
    }else if([model.isMsg isEqualToString:@"1"])
    {
        return 1;
    }else
    {
        return 0;
    }
    
}

//用户-正在申请的
-(void)applyForPhone:(NSString *)phone bdphone:(NSString *)bdphone
{
    NSFetchRequest * fecthR = [NSFetchRequest new];
    NSEntityDescription * entityD = [NSEntityDescription entityForName:@"H_BingDInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR setEntity:entityD];
    NSString * str1 = [NSString stringWithFormat:@"phone LIKE '%@'",phone];
    fecthR.predicate = [NSPredicate predicateWithFormat:str1];
    NSArray * arr1 = [_appDele.managedObjectContext executeFetchRequest:fecthR error:nil];
    H_BingDInfo * model = arr1[0];
    model.bingDphone = bdphone;
    model.isMsg = @"1";
    [_appDele saveContext];
    
    NSFetchRequest * fecthR2 = [NSFetchRequest new];
    NSEntityDescription * entityD2 = [NSEntityDescription entityForName:@"H_BingDInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR2 setEntity:entityD2];
    NSString * str2 = [NSString stringWithFormat:@"phone LIKE '%@'",bdphone];
    fecthR2.predicate = [NSPredicate predicateWithFormat:str2];
    NSArray * arr2 = [_appDele.managedObjectContext executeFetchRequest:fecthR2 error:nil];
    H_BingDInfo * model2 = arr2[0];
    model2.bingDphone = phone;
    model2.isMsg = @"2";
    [_appDele saveContext];

}
//用户-被申请的账号
-(void)beApplyForPhone :(NSString *)phone bdPhone:(NSString *)bdphone B:(BOOL)b
{
    NSFetchRequest * fecthR = [NSFetchRequest new];
    NSEntityDescription * entityD = [NSEntityDescription entityForName:@"H_BingDInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR setEntity:entityD];
    NSString * str1 = [NSString stringWithFormat:@"phone LIKE '%@'",phone];
    fecthR.predicate = [NSPredicate predicateWithFormat:str1];
    NSArray * arr1 = [_appDele.managedObjectContext executeFetchRequest:fecthR error:nil];
    H_BingDInfo * model = arr1[0];
    if (b)
    {
        model.isMsg = @"0";
    }
    else{
        model.isMsg = @"3";
        model.bingDphone = nil;
    }
    [_appDele saveContext];
    
    NSFetchRequest * fecthR2 = [NSFetchRequest new];
    NSEntityDescription * entityD2 = [NSEntityDescription entityForName:@"H_BingDInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR2 setEntity:entityD2];
    NSString * str2 = [NSString stringWithFormat:@"phone LIKE '%@'",bdphone];
    fecthR2.predicate = [NSPredicate predicateWithFormat:str2];
    NSArray * arr2 = [_appDele.managedObjectContext executeFetchRequest:fecthR2 error:nil];
    H_BingDInfo * model2 = arr2[0];
    if (b)
    {
        model2.isMsg = @"0";
    }
    else{
        model2.isMsg = @"3";
        model2.bingDphone = nil;
    }
    [_appDele saveContext];
}
//查询绑定手机号
-(NSString *)checkBDPhone:(NSString *)phone
{
    NSFetchRequest * fecthR = [NSFetchRequest new];
    NSEntityDescription * entityD = [NSEntityDescription entityForName:@"H_BingDInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR setEntity:entityD];
    NSString * str1 = [NSString stringWithFormat:@"phone LIKE '%@'",phone];
    fecthR.predicate = [NSPredicate predicateWithFormat:str1];
    NSArray * arr1 = [_appDele.managedObjectContext executeFetchRequest:fecthR error:nil];
    H_BingDInfo * model = arr1[0];
    return model.bingDphone;

}

//获取用户信息
-(NSDictionary *)getUserInfoFromDataWithPhone:(NSString *)phone
{
    NSMutableDictionary * userInfoDic = [NSMutableDictionary new];
    
    NSFetchRequest * fecthR = [NSFetchRequest new];
    NSEntityDescription * entityD = [NSEntityDescription entityForName:@"H_UsersInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR setEntity:entityD];
    NSString * str1 = [NSString stringWithFormat:@"phone LIKE '%@'",phone];
    fecthR.predicate = [NSPredicate predicateWithFormat:str1];
    NSArray * arr1 = [_appDele.managedObjectContext executeFetchRequest:fecthR error:nil];
    H_UsersInfo * use = arr1[0];
    
    [userInfoDic setObject:use.nick forKey:@"userNick"];
    
    NSFetchRequest * fecthR1 = [NSFetchRequest new];
    NSEntityDescription * entityD1 = [NSEntityDescription entityForName:@"H_userHeadPic" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR1 setEntity:entityD1];
    NSString * str2 = [NSString stringWithFormat:@"phone LIKE '%@'",phone];
    fecthR1.predicate = [NSPredicate predicateWithFormat:str2];
    NSArray * arr2 = [_appDele.managedObjectContext executeFetchRequest:fecthR1 error:nil];
    H_userHeadPic * use1 = arr2[0];
    
    UIImage * image = [UIImage imageWithData:use1.headPic];
    
    [userInfoDic setObject:image forKey:@"headPic"];
    return userInfoDic;
    
}

//用户-更改头像
-(void)changeUserHeadPicWithPhone:(NSString *)phone Head:(NSData *)head
{
    
    NSFetchRequest * fecthR1 = [NSFetchRequest new];
    NSEntityDescription * entityD1 = [NSEntityDescription entityForName:@"H_userHeadPic" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR1 setEntity:entityD1];
    NSString * str2 = [NSString stringWithFormat:@"phone LIKE '%@'",phone];
    fecthR1.predicate = [NSPredicate predicateWithFormat:str2];
    NSArray * arr2 = [_appDele.managedObjectContext executeFetchRequest:fecthR1 error:nil];
    H_userHeadPic * use1 = arr2[0];
    use1.headPic = head;
    [_appDele saveContext];

}
//用户-更改昵称
-(void)changeUserNickWithPhone:(NSString *)phone Nick:(NSString *)nick
{
    NSFetchRequest * fecthR1 = [NSFetchRequest new];
    NSEntityDescription * entityD1 = [NSEntityDescription entityForName:@"H_UsersInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR1 setEntity:entityD1];
    NSString * str2 = [NSString stringWithFormat:@"phone LIKE '%@'",phone];
    fecthR1.predicate = [NSPredicate predicateWithFormat:str2];
    NSArray * arr2 = [_appDele.managedObjectContext executeFetchRequest:fecthR1 error:nil];
    H_UsersInfo * use1 = arr2[0];
    use1.nick = nick;
    [_appDele saveContext];
}
//用户-更改密码
-(void)changeUserHeadPassWordWithPhone:(NSString *)phone Pass:(NSString *)passW
{
    NSFetchRequest * fecthR1 = [NSFetchRequest new];
    NSEntityDescription * entityD1 = [NSEntityDescription entityForName:@"H_UsersInfo" inManagedObjectContext:_appDele.managedObjectContext];
    [fecthR1 setEntity:entityD1];
    NSString * str2 = [NSString stringWithFormat:@"phone LIKE '%@'",phone];
    fecthR1.predicate = [NSPredicate predicateWithFormat:str2];
    NSArray * arr2 = [_appDele.managedObjectContext executeFetchRequest:fecthR1 error:nil];
    H_UsersInfo * use1 = arr2[0];
    use1.password = passW;
    [_appDele saveContext];
}
@end
